<template>
  <div class="ambulance">
    <div class="ambulance-header">
      <el-row>
        <el-col :span="1">
          <el-button
            type="primary"
            v-btnpermission="'btn-ambadd'"
            @click="setData('add')"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1">
          <!-- 导入 -->
          <el-upload
            class="upload"
            :multiple="false"
            :show-file-list="false"
            accept="csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            :http-request="toLead"
          >
            <template #trigger>
              <el-button type="primary" v-btnpermission="'btn-ambload'"
                >导入</el-button
              >
            </template>
          </el-upload>
        </el-col>
      </el-row>
      <el-card class="drug-head-contatiner-title2">
        <el-form :inline="true" :model="formSearch" class="demo-form-inline">
          <el-form-item label="活动">
            <el-select
              v-model="formSearch.activitycode"
              filterable
              placeholder="--选择--"
            >
              <el-option
                v-for="item in activityOption"
                :key="item.Code"
                :label="item.Name"
                :value="item.Code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="所属城市" prop="cityCode">
            <el-tree-select
              v-model="formSearch.cityCode"
              :data="cityTreeOption"
              :props="defaultProps"
              :render-after-expand="false"
              node-key="Code"
              check-strictly
              filterable
              clearable
            />
          </el-form-item>
          <el-form-item label="场馆">
            <el-select
              v-model="formSearch.activityvenuescode"
              placeholder="--选择--"
              filterable
              clearable
            >
              <el-option
                v-for="item in activityVenuesOption"
                :key="item.Code"
                :label="item.CHIDescription"
                :value="item.Code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="车辆状态">
            <el-select
              v-model="formSearch.stateCode"
              filterable
              clearable
              placeholder="--选择--"
            >
              <el-option
                v-for="item in AmbulanceStateOption"
                :key="item.Code"
                :label="item.Name"
                :value="item.Code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="司机">
            <!-- <el-select v-model="formSearch.driver" placeholder="--选择--">
              <el-option
                v-for="item in driverOption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select> -->
            <el-input
              v-model="formSearch.driver"
              placeholder="请输入司机姓名"
            ></el-input>
          </el-form-item>
          <el-form-item label="是否在线">
            <el-select v-model="formSearch.isOnline" placeholder="--选择--">
              <el-option
                v-for="item in isOnlineOption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="实际标识">
            <el-select
              v-model="formSearch.ambulanceName"
              placeholder="--选择--"
              clearable
              filterable
            >
              <el-option
                v-for="item in ambulanceNameOption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
            <el-button @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <div class="ambulance-list">
      <el-row>
        <el-col>
          <!-- 角色列表 -->
          <el-table :data="AmbulanceList" border stripe>
            <!-- 索引列 -->
            <el-table-column
              type="index"
              label="#"
              :index="indexMethod"
            ></el-table-column>
            <el-table-column
              prop="Code"
              label="编码"
              width="80"
            ></el-table-column>
            <el-table-column prop="ActivityName" label="活动"></el-table-column>
            <el-table-column prop="VenusName" label="场馆"></el-table-column>
            <el-table-column
              prop="AmbulanceName"
              label="实际标识"
            ></el-table-column>
            <el-table-column prop="Driver" label="司机"></el-table-column>
            <el-table-column prop="Doctor" label="医生"></el-table-column>
            <el-table-column prop="IsOnline" label="是否在线">
              <template #default="scope">
                <el-switch
                  v-model="scope.row.IsOnline"
                  :active-value="1"
                  :inactive-value="0"
                  disabled
                />
              </template>
            </el-table-column>
            <el-table-column
              prop="StateName"
              label="车辆状态"
            ></el-table-column>
            <el-table-column prop="CityName" label="所属城市"></el-table-column>
            <el-table-column
              prop="Telephone"
              label="联系电话"
            ></el-table-column>
            <el-table-column prop="IsEffective" label="是否有效">
              <template #default="scope">
                <el-switch
                  v-model="scope.row.IsEffective"
                  :active-value="1"
                  :inactive-value="0"
                  disabled
                />
              </template>
            </el-table-column>
            <el-table-column label="操作" width="270">
              <template #default="scope">
                <el-button
                  size="small"
                  type="primary"
                  v-btnpermission="'btn-ambupdate'"
                  :icon="Edit"
                  @click="setData('edit', scope.row)"
                  >编辑</el-button
                >
                <el-button
                  size="small"
                  type="danger"
                  v-btnpermission="'btn-ambdelete'"
                  @click="rowDelete(scope.row.Code)"
                  :icon="Delete"
                  >删除</el-button
                >
                <!-- 导入 -->
                <el-upload
                  class="uploadBtn"
                  :multiple="false"
                  :show-file-list="false"
                  :data="paramLoad(scope.row.Code)"
                  :disabled="scope.row.IfLoadDrugs == 1"
                  accept="csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                  :http-request="toLead1"
                >
                  <template #trigger>
                    <el-button
                      :type="scope.row.IfLoadDrugs ? 'danger' : 'primary'"
                      size="small"
                      :icon="Upload"
                      v-btnpermission="'btn-ambloaddrugs'"
                      >{{
                        scope.row.IfLoadDrugs ? '已导入' : '导入'
                      }}</el-button
                    >
                  </template>
                </el-upload>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12" :offset="8">
          <el-pagination
            background
            layout="total,prev, pager, next"
            v-model:total="formSearch.total"
            v-model:page-size="formSearch.pageSize"
            v-model:current-page="formSearch.page"
            @current-change="handleCurrentChange"
          />
        </el-col>
      </el-row>
      <el-dialog
        :title="dialogType == 'add' ? '新增' : '修改'"
        v-model="addDialogVisible"
        width="50%"
        @close="addDislogClosed"
      >
        <el-form
          :model="addDialogForm"
          :rules="ambulanceFormRulesMixin.addFormRules"
          ref="addDialogRef"
          label-width="80px"
          inline
        >
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="活动" prop="ActivityCode">
                <el-select
                  v-model="addDialogForm.ActivityCode"
                  placeholder="--请选择--"
                >
                  <el-option
                    v-for="item in activityOption"
                    :key="item.Code"
                    :label="item.Name"
                    :value="item.Code"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="所属城市" prop="CityCode">
                <el-tree-select
                  v-model="addDialogForm.CityCode"
                  :data="cityTreeOption"
                  :props="defaultProps"
                  :render-after-expand="false"
                  node-key="Code"
                  check-strictly
                  filterable
                  clearable
                /> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="场馆" prop="ActivityVenuesCode">
                <el-select
                  v-model="addDialogForm.ActivityVenuesCode"
                  size="large"
                  placeholder="--请选择--"
                  filterable
                  clearable
                >
                  <el-option
                    v-for="item in activityVenuesOptionAdd"
                    :key="item.Code"
                    :label="item.CHIDescription"
                    :value="item.Code"
                  />
                </el-select> </el-form-item
            ></el-col>

            <el-col :span="10">
              <el-form-item label="实际标识" prop="Name">
                <el-input v-model="addDialogForm.Name" /> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="编码" prop="Code">
                <el-input v-model="addDialogForm.Code" /> </el-form-item
            ></el-col>

            <el-col :span="10">
              <el-form-item prop="StateCode" label="车辆状态">
                <el-select
                  v-model="addDialogForm.StateCode"
                  placeholder="--选择--"
                  clearable
                  filterable
                >
                  <el-option
                    v-for="item in AmbulanceStateOption"
                    :key="item.Code"
                    :label="item.Name"
                    :value="item.Code"
                  />
                </el-select> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item prop="IsEffective" label="是否有效">
                <el-checkbox
                  v-model="addDialogForm.IsEffective"
                  :true-label="1"
                  :false-label="0"
                />
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="是否在线" prop="IsOnline">
                <el-checkbox
                  v-model="addDialogForm.IsOnline"
                  :true-label="1"
                  :false-label="0"
                /> </el-form-item
            ></el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="经度" prop="Lng" :precision="7" size="large">
                <el-input-number v-model="addDialogForm.Lng" /> </el-form-item
            ></el-col>
            <el-col :span="10">
              <el-form-item label="纬度" prop="Lat" :precision="7" size="large">
                <el-input-number v-model="addDialogForm.Lat" /> </el-form-item
            ></el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="事件编码" prop="EventCode">
                <el-input v-model="addDialogForm.EventCode" /> </el-form-item
            ></el-col>
            <el-col :span="10">
              <el-form-item label="司机" prop="driver">
                <el-select
                  v-model="addDialogForm.DriverCode"
                  clearable
                  filterable
                  placeholder="--请选择--"
                >
                  <el-option
                    v-for="item in driverOption"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select> </el-form-item
            ></el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="医生" prop="Doctor">
                <el-select
                  v-model="addDialogForm.DoctorCode"
                  clearable
                  placeholder="--请选择--"
                  filterable
                >
                  <el-option
                    v-for="item in doctorOption"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="10">
              <el-form-item label="护士" prop="Nurse">
                <el-select
                  v-model="addDialogForm.NurseCode"
                  clearable
                  placeholder="--请选择--"
                  filterable
                >
                  <el-option
                    v-for="item in nurseOption"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select> </el-form-item
            ></el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="担架工" prop="Stretcher">
                <el-select
                  v-model="addDialogForm.StretcherCode"
                  clearable
                  filterable
                  placeholder="--请选择--"
                >
                  <el-option
                    v-for="item in stretcherOption"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="10">
              <el-form-item label="电话号码" prop="Telephone">
                <el-input v-model="addDialogForm.Telephone" /> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="单兵编码" prop="DeviceIndexCode">
                <el-input
                  v-model="addDialogForm.DeviceIndexCode"
                /> </el-form-item
            ></el-col>
          </el-row>
        </el-form>
        <template v-slot:footer>
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addFormSubmit">确 定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from 'vue';
import { Edit, Delete, Upload } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { ambulanceFormRulesMixin } from '@/utils/mixin';
import { importExcel } from '@/utils/excel';
import {
  getAmbulanceList,
  addAmbulance,
  editAmbulance,
  delAmbulance,
  AddAmbulanceList,
  AddAmbulanceListDrugs,
} from '@/network/ambulance';
import {
  getDicByTableName,
  getDicUserByRole,
  getDicVehicle,
  getCityTree,
  getActivityVenues,
} from '@/network/dictionary';
// 导入药品
const paramLoad = (v) => {
  let y = {};
  y.id = v;
  return y;
};
const dialogType = ref('');
//搜索
const formSearch = reactive({
  activitycode: '',
  activityvenuescode: '',
  stateCode: '',
  cityCode: null,
  driver: '',
  isOnline: null,
  ambulanceName: '',
  total: 0,
  page: 1,
  pageSize: 10,
});
//分页索引
const indexMethod = (index) => {
  return index + (formSearch.page - 1) * formSearch.pageSize + 1;
};
//导入Excel表格
const toLead = (e) => {
  importExcel.methods.importExcel(e, (res) => {
    console.log(res, '回调函数Excel');
    if (res.length > 0) {
      AddAmbulanceList(res).then(() => {
        ElMessage.success('导入成功');
        initDict();
        initData();
      });
    } else {
      ElMessage.error('请选择文件');
    }
  });
};
//导入表格
const toLead1 = (e) => {
  importExcel.methods.importExcel(e, (res) => {
    if (res.length > 0) {
      let newLoadOjb = {};
      newLoadOjb.cityInfo = res;
      newLoadOjb.id = e.data.id;
      AddAmbulanceListDrugs(newLoadOjb, 'btn-ambloaddrugs').then(() => {
        ElMessage.success('导入成功');
      });
    } else {
      ElMessage.error('请选择文件');
    }
  });
};
//初始化数据
const AmbulanceList = ref([]);
const initData = () => {
  let params = {
    page: formSearch.page,
    pageSize: formSearch.pageSize,
    ambulanceName: formSearch.ambulanceName,
    activitycode: formSearch.activitycode,
    activityvenuescode: formSearch.activityvenuescode,
    stateCode: formSearch.stateCode,
    driver: formSearch.driver,
    isOnline: formSearch.isOnline,
    cityCode: formSearch.cityCode,
  };
  getAmbulanceList(params).then((res) => {
    AmbulanceList.value = res.data;
    formSearch.total = res.total;
  });
};
const initWatch = () => {
  //监听 --搜索框的联动
  watch(
    [() => formSearch.cityCode, () => formSearch.activityvenuescode],
    (newValue, oldValue) => {
      //城市的变化
      if (newValue[0] != oldValue[0]) {
        if (newValue[0] && newValue[0] != 1) {
          activityVenuesOption.value = activityVenuesOptionConst.filter(
            (it) => it.CityCode == newValue[0],
          );
          ambulanceNameOption.value = ambulanceNameOptionConst.filter(
            (it) => it.CityCode == newValue[0],
          );
        } else {
          activityVenuesOption.value = activityVenuesOptionConst;
          ambulanceNameOption.value = ambulanceNameOptionConst;
        }
        formSearch.ambulanceName = ''; //车辆重置
        formSearch.activityvenuescode = ''; //场馆重置
      }

      //场馆的变化
      if (newValue[1] != oldValue[1]) {
        if (newValue[1]) {
          ambulanceNameOption.value = ambulanceNameOptionConst.filter(
            (it) => it.VenuesCode == newValue[1],
          );
        } else {
          //判断城市是否为Null
          if (newValue[0] && newValue[0] != 1) {
            ambulanceNameOption.value = ambulanceNameOptionConst.filter(
              (it) => it.CityCode == newValue[0],
            );
          } else {
            ambulanceNameOption.value = ambulanceNameOptionConst;
          }
        }
        formSearch.ambulanceName = ''; //车辆重置
      }
    },
    {
      deep: true,
    },
  );
  //监听 --新增页面的联动
  watch(
    [
      () => addDialogForm.value.CityCode,
      () => addDialogForm.value.ActivityVenuesCode,
    ],
    (newValue, oldValue) => {
      //城市场馆同时变化，修改赋值
      if (newValue[0] != oldValue[0] && newValue[1] != oldValue[1]) {
        return;
      }
      //城市的变化
      if (newValue[0] != oldValue[0]) {
        if (newValue[0] && newValue[0] != 1) {
          activityVenuesOptionAdd.value = activityVenuesOptionConst.filter(
            (it) => it.CityCode == newValue[0],
          );
          //司机过滤
          driverOption.value = driverOptionConst.filter(
            (it) => it.CityCode == newValue[0],
          );
          //医生过滤
          doctorOption.value = doctorOptionConst.filter(
            (it) => it.CityCode == newValue[0],
          );
          //护士过滤
          nurseOption.value = nurseOptionConst.filter(
            (it) => it.CityCode == newValue[0],
          );
          //担架工过滤
          stretcherOption.value = stretcherOptionConst.filter(
            (it) => it.CityCode == newValue[0],
          );
        } else {
          activityVenuesOption.value = activityVenuesOptionConst;
          driverOption.value = driverOptionConst;
          doctorOption.value = doctorOptionConst;
          nurseOption.value = nurseOptionConst;
          stretcherOption.value = stretcherOptionConst;
        }

        addDialogForm.value.Driver = ''; //司机重置
        addDialogForm.value.Doctor = ''; //医生重置
        addDialogForm.value.Nurse = ''; //护士重置
        addDialogForm.value.Stretcher = ''; //担架工重置
        addDialogForm.value.ActivityVenuesCode = ''; //场馆重置
      }

      //场馆的变化
      if (newValue[1] != oldValue[1]) {
        if (newValue[1]) {
          //司机过滤
          driverOption.value = driverOptionConst.filter(
            (it) => it.VenuesCode == newValue[1],
          );
          //医生过滤
          doctorOption.value = doctorOptionConst.filter(
            (it) => it.VenuesCode == newValue[1],
          );
          //护士过滤
          nurseOption.value = nurseOptionConst.filter(
            (it) => it.VenuesCode == newValue[1],
          );
          //担架工过滤
          stretcherOption.value = stretcherOptionConst.filter(
            (it) => it.VenuesCode == newValue[1],
          );
        } else {
          //判断城市是否为Null
          if (newValue[0] && newValue[0] != 1) {
            activityVenuesOption.value = activityVenuesOptionConst.filter(
              (it) => it.CityCode == newValue[0],
            );
            //司机过滤
            driverOption.value = driverOptionConst.filter(
              (it) => it.CityCode == newValue[0],
            );
            //医生过滤
            doctorOption.value = doctorOptionConst.filter(
              (it) => it.CityCode == newValue[0],
            );
            //护士过滤
            nurseOption.value = nurseOptionConst.filter(
              (it) => it.CityCode == newValue[0],
            );
            //担架工过滤
            stretcherOption.value = stretcherOptionConst.filter(
              (it) => it.CityCode == newValue[0],
            );
          } else {
            activityVenuesOption.value = activityVenuesOptionConst;
            driverOption.value = driverOptionConst;
            doctorOption.value = doctorOptionConst;
            nurseOption.value = nurseOptionConst;
            stretcherOption.value = stretcherOptionConst;
          }
        }
        addDialogForm.value.Driver = ''; //
        addDialogForm.value.Doctor = ''; //
        addDialogForm.value.Nurse = ''; //
        addDialogForm.value.Stretcher = ''; //
      }
    },
    {
      deep: true,
    },
  );
};
//初始化字典
const activityOption = ref([]);
const activityVenuesOption = ref([]);
const AmbulanceStateOption = ref([]);
const ambulanceNameOption = ref([]);
const cityTreeOption = ref([]);
const activityVenuesOptionAdd = ref([]);
const activityVenuesOptionUpdate = ref([]);
const isOnlineOption = ref([
  {
    value: 1,
    label: '在线',
  },
  {
    value: 0,
    label: '离线',
  },
]);
let driverOptionConst = [];
let nurseOptionConst = [];
let doctorOptionConst = [];
let stretcherOptionConst = [];
let driverOption = ref([]);
let nurseOption = ref([]);
let stretcherOption = ref([]);
let doctorOption = ref([]);
const defaultProps = {
  children: 'Children',
  label: 'Name',
  value: 'Code',
};
//定义内存变量
let activityVenuesOptionConst = []; //场馆
let ambulanceNameOptionConst = []; //车辆实际标识
const initDict = async () => {
  await getDicByTableName({ tableName: 'tactivity' }).then((res) => {
    activityOption.value = res;
  });
  await getActivityVenues().then((res) => {
    activityVenuesOptionConst = res;
    activityVenuesOption.value = res;
    activityVenuesOptionAdd.value = res;
    activityVenuesOptionUpdate.value = res;
  });
  await getDicByTableName({ tableName: 'tzambulancestate' }).then((res) => {
    AmbulanceStateOption.value = res;
  });
  //从人员表取司机
  await getDicUserByRole({ role: '4' }).then((res) => {
    driverOption.value = res;
    driverOptionConst = res;
  });
  //从人员表取护士
  await getDicUserByRole({ role: '6' }).then((res) => {
    nurseOption.value = res;
    nurseOptionConst = res;
  });
  //从人员表取担架工
  await getDicUserByRole({ role: 'PersonnelType-5' }).then((res) => {
    stretcherOption.value = res;
    stretcherOptionConst = res;
  });
  //从人员表取医生
  await getDicUserByRole({ role: '5' }).then((res) => {
    doctorOption.value = res;
    doctorOptionConst = res;
  });
  //获取实际标识
  await getDicVehicle().then((res) => {
    ambulanceNameOptionConst = res;
    ambulanceNameOption.value = res;
  });

  //获取城市
  await getCityTree().then((res) => {
    cityTreeOption.value = res;
  });
  initWatch();
};
onMounted(() => {
  initDict();
  initData();
});
//搜索
const onSubmit = () => {
  formSearch.page = 1;
  initData();
};
//重置
const onReset = () => {
  formSearch.activitycode = '';
  formSearch.activityvenuescode = '';
  formSearch.stateCode = '';
  formSearch.driver = '';
  formSearch.isOnline = '';
  formSearch.ambulanceName = '';
  formSearch.page = 1;
  initData();
};
//分页
const handleCurrentChange = (val) => {
  formSearch.page = val;
  initData();
};
//新增、修改
const setData = (type, row) => {
  dialogType.value = type;
  addDialogVisible.value = true;
  type == 'add'
    ? (addDialogForm.value = {})
    : (addDialogForm.value = { ...row });
  addDialogForm.value.originAmbcode = row?.Code;
};
//新增
const addDialogVisible = ref(false);
const addDialogRef = ref(null);
let addDialogForm = ref({
  ActivityCode: null,
  ActivityVenuesCode: null,
  StateCode: null,
  IsOnline: 1,
  Name: '',
  Driver: '',
  Doctor: '',
  Nurse: '',
  Stretcher: '',
  DriverCode: null,
  DoctorCode: null,
  NurseCode: null,
  StretcherCode: null,
  Telephone: '',
  Code: '',
  CityCode: null,
  Lng: 0.0,
  Lat: 0.0,
  EventCode: '',
  StateName: '',
  IsEffective: 1,
  DeviceIndexCode: null,
  originAmbcode: null,
});
const addFormSubmit = async () => {
  await addDialogRef.value.validate(async (valid) => {
    if (valid) {
      //给司机、医生护士、进行赋值
      addDialogForm.value.Driver =
        addDialogForm.value.DriverCode &&
        driverOptionConst.find((i) => i.value == addDialogForm.value.DriverCode)
          ?.label;
      addDialogForm.value.Nurse =
        addDialogForm.value.NurseCode &&
        nurseOptionConst.find((i) => i.value == addDialogForm.value.NurseCode)
          ?.label;
      addDialogForm.value.Doctor =
        addDialogForm.value.DoctorCode &&
        doctorOptionConst.find((i) => i.value == addDialogForm.value.DoctorCode)
          ?.label;
      addDialogForm.value.Stretcher =
        addDialogForm.value.StretcherCode &&
        stretcherOptionConst.find(
          (i) => i.value == addDialogForm.value.StretcherCode,
        )?.label;
      if (dialogType.value === 'add') {
        await addAmbulance(addDialogForm.value)
          .then(() => {
            addDialogVisible.value = false;
            initData();
            initDict();
            ElMessage.success('新增成功');
          })
          .catch((err) => {
            console.log(err);
            ElMessage.error('新增失败');
          });
      } else {
        await editAmbulance(addDialogForm.value)
          .then(() => {
            addDialogVisible.value = false;
            initData();
            initDict();
            ElMessage.success('修改成功');
          })
          .catch((err) => {
            console.log(err);
            ElMessage.error('修改失败');
          });
      }
    } else {
      ElMessage.error('请按要求填写');
      return false;
    }
  });
};
const addDislogClosed = () => {
  addDialogRef.value.resetFields();
  addDialogRef.value.clearValidate();
};

//删除
const rowDelete = async (id) => {
  await ElMessageBox.confirm('确定删除该条记录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async (action) => {
    if (action === 'confirm') {
      await delAmbulance({ code: id })
        .then(() => {
          initData();
          initDict();
          ElMessage.success('删除成功');
        })
        .catch((err) => {
          console.log(err);
          ElMessage.success('删除失败');
        });
    }
  });
};
</script>

<style lang="scss" scoped>
.ambulance {
  margin: 14px 15px 14px;
  .ambulance-header {
    .drug-head-contatiner-title2 {
      font-size: 18px;
      font-weight: bold;
      color: #333;
      margin-top: 14px;
    }
  }
  .ambulance-list {
    margin-top: 14px;
  }
}
.uploadBtn {
  margin: 10px;
  display: inline;
}
</style>
